import React, {createContext, useState} from "react";
import {useAliveController} from 'react-activation';

interface RefreshContextType {
    outletShow: boolean;
    updateOutletShow: (val: boolean) => void;
    refreshScopeOutletShow: (pathname: string) => void;
}

export const RefreshContext = createContext<RefreshContextType>({
    outletShow: true,
    updateOutletShow: () => {
    },
    refreshScopeOutletShow: () => {
    }
});

export const RefreshProvider: React.FC<{ children: React.ReactNode }> = ({children}) => {
    const [outletShow, setOutletShow] = useState(true);
    const {refresh} = useAliveController()
    const updateOutletShow = (val: boolean) => {
        setOutletShow(val);
    };
    const refreshScopeOutletShow = async (pathname: string) => {
        refresh && await refresh(pathname)
    }
    const contextValue = {
        outletShow,
        updateOutletShow,
        refreshScopeOutletShow
    };

    return <RefreshContext.Provider value={contextValue}>{children}</RefreshContext.Provider>;
};
